<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>非受控组件</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="../js/prop-types.js"></script>
    <style>
        
    </style>

</head>
<body>
    <div id="test"></div>
    <script type="text/babel">
    /* 
        受控组件
            表单中输入类的dom,随着用户的输入，将值收集到state中，那么就成为受控组件
    */
        class Login extends React.Component{
            state={
                username:"",
                password:"123"
            }
            render(){
                return(
                    <form onSubmit={this.handleLogin}>
                        用户名：<input type="text" onChange={this.saveUsername}/><br />
                        密码：<input type="password" onChange={this.savePassword}/><br/>
                        <button>登录</button>
                    </form>
                )
            }
            // 保存用户名到state
            saveUsername=(e)=>{
                this.setState({username:e.target.value})
            }
            // 保存密码到state
            savePassword=(e)=>{
                this.setState({password:e.target.value})
            }
            handleLogin=(e)=>{
                e.preventDefault()
                const {username,password} =this.state
                alert(`用户名是${username}，密码是${password}`)
            }
        }
        ReactDOM.render(<Login/>,document.getElementById('test'))
    </script>
    
</body>
</html>